package cn.hefan.service;

import java.sql.SQLException;
import java.util.List;

import cn.hefan.dao.findUserDao;
import cn.hefan.domain.Pages;
import cn.hefan.domain.ResultInfo;
import cn.hefan.domain.User;

public class findUserService {

	public static Pages<User> findAll(int currentPage,int pageSize) {
		Pages<User> page = new Pages<User>();
		//设置当前页码
		page.setCurrentPage(currentPage);
		//设置每页显示数据数
		page.setPageSize(pageSize);
		//设置总记录数
		int allCount = 0;
		try {
				allCount = findUserDao.findCount();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		page.setAllCount(allCount);
		//设置总页数
		int allPages = allCount % pageSize ==0? allCount/pageSize : allCount/pageSize+1;
		page.setAllPages(allPages);
		
		//设置每页显示的集合
		int start = (currentPage-1)*pageSize;
		List<User> user = null;
		try {
			user = findUserDao.findUser(start,pageSize);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		page.setList(user);
		return page;
	}

	public static Pages<User> findDivide(String divide, int currentPage, int pageSize) {
		
		Pages<User> page = new Pages<User>();
		//设置当前页码
		page.setCurrentPage(currentPage);
		//设置每页显示数据数
		page.setPageSize(pageSize);
		//设置总记录数
		int allCount = 0;
		try {
				allCount = findUserDao.findDicount(divide);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		page.setAllCount(allCount);
		//设置总页数
		int allPages = allCount % pageSize ==0? allCount/pageSize : allCount/pageSize+1;
		page.setAllPages(allPages);
		
		//设置每页显示的集合
		int start = (currentPage-1)*pageSize;
		List<User> user = null;
		try {
			user = findUserDao.findDiUser(divide, start, pageSize);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		page.setList(user);
		return page;
	}

	public static List<User> finUname(String name) {
		List<User> users= null;
		try {
		 users = findUserDao.finUname(name);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return users;
	}

	public static ResultInfo addUser(User user) {
		int i = 0;
		ResultInfo info = null;
		try {
			i = findUserDao.addUser(user);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(i==1) {
			info = new ResultInfo(true);
		}else {
			info = new ResultInfo(false,"新增失败");
		}
		return info;
	}

	public static void deletUser(String username) {
		try {
			findUserDao.delectUser(username);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


}
